/*
 *
 *  Copyright (C) 2008 GSyC/LibreSoft
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see http://www.gnu.org/licenses/. 
 *
 *  Author : Roberto Calvo Palomino <rocapal@gsyc.es>
 *
 */


package com.LibreGeoSocial.App;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.LibreGeoSocial.App.AR.ARView;
import com.LibreGeoSocial.App.Resources.PanoramioItem;
import com.LibreGeoSocial.App.Resources.ResourceItem;
import com.LibreGeoSocial.App.Resources.ResourceManager;


public class PhotoList extends ListActivity {

	private static final int MENU_SEE_CAM = Menu.FIRST + 1;

	PhotoAdapter mAdapter; 
	
	public void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		    

		if(!ResourceManager.getInstance().refreshList(ResourceManager.PHOTO_RESOURCE))
			Toast.makeText(getBaseContext(), 
					ResourceManager.getInstance().getMessage(), 
					Toast.LENGTH_LONG).show();
		
  		// Adapter for ListView
        mAdapter = new PhotoAdapter(this);
        setListAdapter(mAdapter);

		
	}
	
    protected void onListItemClick(ListView l, View v, int position, long id) {
        
    	ResourceItem resource = ResourceManager.getInstance().getResource(position,
				  ResourceManager.PHOTO_RESOURCE);

    	Intent i = new Intent(this, ViewMap.class);
    	
    	i.putExtra("latitude",resource.getLatitude());
    	i.putExtra("longitude",resource.getLongitude());
    	i.putExtra("url",resource.getUrl());
    	
    	startActivity(i);
    
	}
	
	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	
        super.onCreateOptionsMenu(menu);
        menu.add(0, MENU_SEE_CAM, 0, "Real View")
        .setIcon(R.drawable.real_view); 
        return true;
    }
	
	
    @Override
    public boolean onOptionsItemSelected (MenuItem item) {
    	
    	switch (item.getItemId()) {
    		case MENU_SEE_CAM:
    			
    			Intent i = new Intent(this, ARView.class);
    	    	
    			i.putExtra("RESOURCE_TYPE", ResourceManager.PHOTO_RESOURCE);
    	    	startActivity(i);
    			
    			break;
    	}
    			  
    	return super.onOptionsItemSelected(item);
    }

    
	public static class PhotoAdapter extends BaseAdapter {

		private Context mContext;
		
		public PhotoAdapter(Context c) {
            mContext = c;
        }

		public int getCount() {
			// TODO Auto-generated method stub
			return ResourceManager.getInstance().getSize(ResourceManager.PHOTO_RESOURCE);
		}

		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			View view;
			
	        if (convertView == null) {
	            // Make up a new view
	            LayoutInflater inflater = (LayoutInflater) mContext
	                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	            view = inflater.inflate(R.layout.generic_list, null);
	        } else {
	            // Use convertView if it is available
	            view = convertView;
	        }
			
	        
	        ResourceItem resource = 
	        	ResourceManager.getInstance().getResource(position, ResourceManager.PHOTO_RESOURCE);
	        
	        if (resource.getService().equals(ResourceManager.PHOTO_RESOURCE))
	        {
	        	PanoramioItem photo = (PanoramioItem) resource;

		        ImageView img = (ImageView) view.findViewById(R.id.image);
		        
				img.setImageBitmap(photo.getBitmapThumb());
				
				TextView t = (TextView) view.findViewById(R.id.title);
				t.setText(photo.getTitle());
				
				t = (TextView) view.findViewById(R.id.owner);
				t.setText(photo.getOwner());
		        
				return view;
	        }
	        
	        return null;
		}

	}



}
